package com.aios.dao;

import com.aios.entity.Door;
import com.aios.entity.DoorExample;
import com.aios.entity.mapper.AssignedState;
import com.aios.entity.mapper.DoorHasUsers;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

public interface DoorMapper {

	int countByExample(DoorExample example);

	int deleteByExample(DoorExample example);

	int deleteByPrimaryKey(Integer id);

	int insert(Door record);

	int insertSelective(Door record);

	List<Door> selectByExample(DoorExample example);

	Door selectByPrimaryKey(Integer id);

	int updateByExampleSelective(@Param("record") Door record,
			@Param("example") DoorExample example);

	int updateByExample(@Param("record") Door record,
			@Param("example") DoorExample example);

	int updateByPrimaryKeySelective(Door record);

	int updateByPrimaryKey(Door record);

	@Select("SELECT * FROM door WHERE STATUS = #{status} ORDER BY SORT_ORDER")
	@ResultMap(value = "MiniResultMap")
	List<Door> findAllByStatus(@Param("status") Integer status);

	@Select("SELECT * FROM door WHERE DOOR_NAME = #{doorName}")
	@ResultMap(value = "MiniResultMap")
	List<Door> findByDoorName(@Param("doorName") String doorName);

	@Select("SELECT ug.ID as ID, ug.GROUP_NAME as NAME, true as STATE "
			+ "FROM user_group_has_door ughd, user_group ug "
			+ "WHERE ughd.USER_GROUP_ID = ug.ID AND ughd.DOOR_ID=#{doorId} "
			+ "ORDER BY ug.SORT_ORDER")
	@ResultMap(value = "DoorStateResultMap")
	List<AssignedState> findAssignedGroups(@Param("doorId") Integer id);

	@Select("SELECT iu.ID as ID, iu.NAME as NAME, true as STATE "
			+ "FROM iris_user_has_door iuhd, iris_user iu "
			+ "WHERE iuhd.IRIS_USER_ID = iu.ID AND iuhd.DOOR_ID=#{doorId} "
			+ "ORDER BY iu.SORT_ORDER")
	@ResultMap(value = "DoorStateResultMap")
	List<AssignedState> findAssignedUsers(Integer doorId);

	@Select("SELECT * FROM door WHERE STATUS = #{status} ORDER BY SORT_ORDER")
	@ResultMap(value = "DoorHasUsersResultMap")
	List<DoorHasUsers> findUsersForDoors(@Param("status") Integer status);

	@Select("SELECT * FROM door WHERE STATUS = #{status} ORDER BY SORT_ORDER LIMIT #{start}, #{size}")
	@ResultMap(value = "DoorHasUsersResultMap")
	List<DoorHasUsers> findPaginatedList(@Param("status") Integer status,
			@Param("start") Integer start, @Param("size") Integer size);

	@Select("SELECT COUNT(*) FROM door WHERE STATUS = #{status}")
	Integer countAllByStatus(@Param("status") Integer status);

	@Select("SELECT ug.ID,ug.GROUP_NAME AS NAME, false as STATE FROM user_group ug "
			+ "WHERE ug.ID NOT IN "
			+ "(SELECT ughd.USER_GROUP_ID FROM user_group_has_door ughd "
			+ "WHERE ughd.DOOR_ID = #{id} GROUP BY ughd.USER_GROUP_ID)")
	@ResultMap(value = "UserGroupListResultMap")
	List<AssignedState> findAvailableGroups(@Param("id") Integer id);

	@Select("SELECT iu.ID,iu.NAME AS NAME, false as STATE FROM iris_user iu "
			+ "WHERE iu.ID NOT IN "
			+ "(SELECT iuhs.IRIS_USER_ID FROM iris_user_has_door iuhs "
			+ "WHERE iuhs.DOOR_ID = #{id} GROUP BY iuhs.IRIS_USER_ID)")
	@ResultMap(value = "IrisUserListResultMap")
	List<AssignedState> findAvailableUsers(@Param("id") Integer id);

	@Select("SELECT * FROM door WHERE ID = #{id}")
	@ResultMap(value = "DoorHasUsersResultMap")
	DoorHasUsers findByIdWithUserAndGroup(@Param("id") Integer id);
	
	@Select("SELECT * FROM door WHERE DOOR_NAME=#{doorName} AND ID != #{id}")
	@ResultMap(value = "MiniResultMap")
	List<Door> findDoorNameNotWithId(@Param("doorName") String doorName, @Param("id") Integer id);

}